import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/home/homeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    meta: {
      keepAlive: true,
      Tabbar: true,
      isLogin: false // 添加 isLogin 属性
    },
    component: HomeView
  }, {
    path: '/order',
    name: 'order',
    component: () => import('../views/other/orderView.vue'),
    meta: {
      keepAlive: true,
      Tabbar: true,
      isLogin: true // 添加 isLogin 属性
    },
  },
  {
    path: '/my',
    name: 'my',
    meta: {
      keepAlive: true,
      Tabbar: true,
      isLogin: true // 添加 isLogin 属性
    },
    component: () => import('../views/my/myView.vue')
  },
  {
    path: '/car',
    name: 'car',
    meta: {
      keepAlive: true,
      Tabbar: false,
      isLogin: true // 添加 isLogin 属性
    },
    component: () => import('../views/car/car.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/other/loginView.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/other/registerView.vue')
  }, {
    path: '/Cards',
    name: 'Cards',
    component: () => import('../views/other/CardsView.vue')
  }, {
    path: '/address',
    name: 'address',
    component: () => import('../views/other/addressView.vue')
  }, {
    path: '/pushaddress',
    name: 'pushaddress',
    component: () => import('../views/other/pushaddress.vue')
  }, {
    path: '/pushaddress',
    name: 'pushaddress',
    component: () => import('../views/other/pushaddress.vue')
  }, {
    path: '/aboutUs',
    name: 'aboutUs',
    component: () => import('../views/other/aboutUs.vue')
  }, {
    path: '/invite',
    name: 'invite',
    component: () => import('../views/other/invite.vue')
  }, {
    path: '/vip',
    name: 'vip',
    component: () => import('../views/other/vip.vue')
  }, {
    path: '/question',
    name: 'question',
    component: () => import('../views/other/question.vue')
  }, {
    path: '/forget',
    name: 'forget',
    component: () => import('../views/other/forget.vue')
  },
  {
    path: '/sureOrder',
    name: 'sureOrder',
    component: () => import('../views/other/sureOrder.vue'),
    meta: {
      keepAlive: true,
      Tabbar: false,
      isLogin: true // 添加 isLogin 属性
    }
  },{
    path: '/updataddress',
    name: 'updataddress',
    component: () => import('../views/other/updataddress.vue'),
    meta: {
      keepAlive: true,
      Tabbar: false,
      isLogin: true // 添加 isLogin 属性
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.path === '/login' || to.path === '/register' || to.path === '/') {
    next()
  } else {
    if (token === null) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router